<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Menu_model.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-02-09
 * Time: 15:25
 */
class Menu_model extends CI_Model {
    public $menu_name;          //菜单名称
    public $menu_parent;        //父级菜单id
    //public $menu_parentall;     //所有父级菜单id
    //public $menu_ischild;       //是否有子菜单
    //public $menu_childall;      //所有子菜单id
    public $menu_controller;    //控制器
    public $menu_action;        //操作方法
    public $menu_sort;          //排序
    public $menu_status;        //状态

    /**
     * get_all_menu
     * 获取全部管理后台菜单数据
     *
     * @return mixed
     */
    public function get_all_menu()
    {
        return $this->db->order_by('menu_sort DESC')->get('admin_menu')->result_array();
    }

    /**
     * get_single_menu
     * 获取指定id的单条管理后台菜单数据
     *
     * @param int $menu_id 菜单id
     * @return mixed
     */
    public function get_single_menu($menu_id)
    {
        $table = $this->db->dbprefix('admin_menu');
        return $this->db
            ->query('select * from ' . $table . ' where menu_id=' . $menu_id)
            ->row_array();
    }

    public function menu_save()
    {
        // 获取表单数据
        $form = $this->input->post();

        $data = array(
            'menu_name' => $form['menu_name'],
            'menu_parentid' => $form['menu_parentid'],
            'menu_module' => $form['menu_module'],
            'menu_controller' => $form['menu_controller'],
            'menu_action' => $form['menu_action'],
            'menu_status' => $form['menu_status']
        );

        // 判断主键ID是否存在，存在则表示更新数据
        if (!array_key_exists('menu_id', $form)) {
            // 新增数据
            $req = $this->db->insert('admin_menu', $data);
        } else {
            // 更新数据
            $req = $this->db->where('menu_id', $form['menu_id'])->update('admin_menu', $data);
        }

        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功！', 'result'=>array('jumpUrl'=>site_url('system/Menu/index')));
        } else {
            return array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
    }

    /**
     * menu_sort
     * 批量更新管理后台系统菜单排序
     *
     * @return bool
     */
    public function menu_sort()
    {
        $form = $this->input->post();

        foreach ($form['menu_sort'] as $key=>$val) {
            $val = !empty($val) ? $val : 0;
            $this->db->where('menu_id', $key)->update('admin_menu', array('menu_sort'=>$val));
        }

        return true;
    }

    /**
     * menu_delete
     * 删除管理后台系统菜单数据
     *
     * @return array
     */
    public function menu_delete()
    {
        $menu_id = $this->input->get('menu_id');

        // 查询需要删除的菜单下是否有隶属子菜单
        $menu = $this->db
            ->from('admin_menu')
            ->where('menu_parentid', $menu_id)
            ->count_all_results();

        if ($menu) return array('status'=>-1, 'msg'=>'该菜单下还有隶属子菜单存在，不能删除', 'result'=>'');

        $req = $this->db->where('menu_id', $menu_id)->delete('admin_menu');

        if ($req) {
            return array('status'=>1, 'msg'=>'操作成功！', 'result'=>'');
        } else {
            return array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
    }
}